package com.example.login_demo.common.http;

import com.example.easyhttp.EasyCallBack;
import com.example.easyhttp.EasyHttp;


import java.util.HashMap;
import java.util.Map;

public class EasyHttpUtils<T>{

    private static EasyHttpUtils instance;

    public static EasyHttpUtils getInstance() {
        if (null == instance) {
            instance = new EasyHttpUtils();
        }
        return instance;
    }


    public  void get(String url, Map<String, String> paramsMap, EasyCallBack callBack) {
        getData(url, paramsMap, callBack);
    }

    public  void get(String url, EasyCallBack callBack) {
        get(url, null, callBack);
    }


    public  void post(String url, EasyCallBack callBack) {
        post(url, null, callBack);
    }


    public  void post(String url, Map<String, String> paramsMap, EasyCallBack callBack) {
        postData(url, paramsMap, null, callBack);
    }

    private void postData(String url, Map<String, String> paramsMap, Object o, EasyCallBack callBack) {
        Map<String, String> headerMap = new HashMap<>();

        Map<String, String> basicParamsMap = getBasicParameters();
        if (paramsMap != null) {
            basicParamsMap.putAll(paramsMap);
        }

        EasyHttp.post(url, basicParamsMap, callBack);

    }


    /**
     * 访问数据
     *
     * @param url
     * @param paramsMap
     * @param callBack
     */
    private static void getData(String url, Map<String, String> paramsMap, EasyCallBack callBack) {
        Map<String, String> headerMap = new HashMap<>();

        Map<String, String> basicParamsMap = getBasicParameters();
        if (paramsMap != null) {
            basicParamsMap.putAll(paramsMap);
        }
       EasyHttp.get(url, basicParamsMap,callBack);
    }


    /**
     * 获取基础参数
     *
     * @return
     */
    private static Map<String, String> getBasicParameters() {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("access_token", "bqUTUgWZM8nJBazwKa8V9Qb-Sj-K5x8A");
        paramsMap.put("timestamp", System.currentTimeMillis() + "");
        paramsMap.put("app_version_code", "57");
        paramsMap.put("platform", "1");
        paramsMap.put("channel", "moego");
        paramsMap.put("app_version_name", "3.1.2");
        paramsMap.put("device_id", "6888970215423ec8");
        paramsMap.put("version_name", "3.1.2");
        //        paramsMap.put("__debug__","1");
        return paramsMap;
    }


}
